package com.example.spring_boot0815.Controller;

import com.example.spring_boot0815.model.Userinfo;
import com.example.spring_boot0815.service.UserService;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    //@Transactional(isolation = Isolation.SERIALIZABLE) 不插入数据
//    @Transactional(propagation = Propagation.SUPPORTS)  插入两条数据
    @Transactional(propagation = Propagation.REQUIRED)
    public int add() {
        //1.非空判断
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername("张五");
        userinfo.setPassword("222");

        //2.调用 service 执行添加
        int result = userService.add(userinfo);
        System.out.println("result:" + result);

        try {
            int num = 10/0;
        } catch (Exception e) {
            //throw e;
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        //3.将结果给前端
        return 0;
    }
}
